package com.zxl.cityservice.controller;

import com.google.gson.Gson;
import com.zxl.cityservice.entity.Buyer;
import com.zxl.cityservice.service.AllService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by bearworker on 17-4-23.
 */
@Controller
@RequestMapping("all")
public class AllController {
    Logger logger = Logger.getLogger(AllController.class);

    Gson gson = new Gson();

    @Autowired
    private AllService allService;

    @RequestMapping("buy")
    public ModelAndView buy(String username){
        logger.debug("username"+username);
        ModelAndView mv = new ModelAndView("buy/buyticket");
        mv.addObject("username",username);
        return mv;
    }

    @RequestMapping("ticket")
    public ModelAndView buyticket(String name,String cellno,String cardno,String price){
        logger.debug("name:"+name);
        ModelAndView mv = new ModelAndView("buy/success");
        Buyer buyer = new Buyer();
        buyer.setName(name);
        buyer.setCellno(cellno);
        buyer.setCardno(cardno);
        buyer.setPrice(price);
        allService.save(buyer);
        mv.addObject("msg","购买成功");
        return mv;
    }

    @RequestMapping("appticket")
    @ResponseBody
    public String appticket(String name,String cellno,String cardno,String price){
        logger.debug("name:"+name);
        Buyer buyer = new Buyer();
        buyer.setName(name);
        buyer.setCellno(cellno);
        buyer.setCardno(cardno);
        buyer.setPrice(price);
        allService.save(buyer);

        Map map = new HashedMap();
        map.put("code",0);
        map.put("msg","购买成功");
        return gson.toJson(map);
    }


    @RequestMapping("report")
    public ModelAndView report(){
        ModelAndView mv = new ModelAndView("buy/buyReport");
        return mv;
    }


    @RequestMapping("allticket")
    @ResponseBody
    public String getAllticket(){
        List<Buyer> list = allService.getAll(0,100000000);

        return gson.toJson(list);
    }


}
